﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RestSharp;
using Newtonsoft.Json;

namespace mytest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int x = (System.Windows.Forms.SystemInformation.WorkingArea.Width - this.Size.Width) / 2;
            int y = (System.Windows.Forms.SystemInformation.WorkingArea.Height - this.Size.Height) / 2;
            this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
            this.Location = (Point)new Size(x, y);         //窗体的起始位置为(x,y)
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            String username = textBox1.Text;
            String password = textBox2.Text;
            var client = new RestClient("http://localhost:1010");

            var request = new RestRequest("api/dotnet/login", Method.POST);
            request.AddParameter("username", username); // adds to POST or URL querystring based on Method
            request.AddParameter("password", password);

            // execute the request
            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string
            
            var jsonObj = JsonConvert.DeserializeObject<dynamic>(content);
            Console.WriteLine(jsonObj);

            if(jsonObj.error != true)
            {
                MessageBox.Show("登录失败，请检查输入信息");
            }
            else
            {
                Form2 f2 = new Form2();
                f2.Show();
                this.Hide();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            WindowState = FormWindowState.Normal;

            this.Activate();

            this.ShowInTaskbar = true;

            notifyIcon1.Visible = false;
        }
    }
}
